<template>
  <div class="citys" ref="wrapper">
    <div>
      <div class="sction">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-items">{{ this.$store.state.city }}</div>
        </div>
      </div>
      <div class="sction">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div
            class="button-items"
            v-for="item in hot"
            :key="item.id"
            @click="handleCity(item.name)"
          >
            {{ item.name }}
          </div>
        </div>
      </div>
      <div class="sction" v-for="(value, key) in cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{ key }}</div>
        <div class="city-name-list">
          <div
            class="city-name-items border-bottom"
            v-for="innervalue in value"
            :key="innervalue.id"
            @click="handleCity(innervalue.name)"
          >
            {{ innervalue.name }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from "better-scroll";
import { mapState, mapMutations } from "vuex";
export default {
  name: "CityList",
  props: {
    cities: Object,
    hot: Array,
    target: String,
  },
  methods: {
    ...mapMutations("dataStore", ["changeCity2"]),
    handleCity(city) {
      // this.$store.dispatch('changeCity', city)
      // this.$store.commit('changeCity', city)
      this.changeCity2(city);
      this.$router.push("/"); //编程式导航
    },
    // ...mapMutations(['changeCity'])
    
  },
  mounted() {
    this.scroll = new Bscroll(this.$refs.wrapper);
  },
  watch: {
    target() {
      if (this.target) {
        const ele = this.$refs[this.target][0];
        this.scroll.scrollToElement(ele);
      }
    },
  },
};
</script>

<style scoped lang="stylus">
@import '~styles/varibles.styl';

.border-topbottom {
  &:before {
    border-color: #ccc;
  }

  &:after {
    border-color: #ccc;
  }
}

.border-bottom {
  &:before {
    border-color: #ccc;
  }
}

.citys {
  position: absolute;
  top: 1.58rem;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: hidden;

  .title {
    line-height: 0.62rem;
    background: #eee;
    padding-left: 0.2rem;
    color: #666;
    font-size: 0.26rem;
  }

  .button-list {
    padding: 0.1rem 0.6rem 0.1rem 0.1rem;
    display: flex;
    flex-wrap: wrap;

    .button-items {
      width: 30%;
      text-align: center;
      border: 0.02rem solid #ccc;
      border-radius: 0.06rem;
      margin: 0.1rem;
      padding: 0.1rem 0;
      box-sizing: border-box;
    }
  }

  .city-name-list {
    .city-name-items {
      line-height: 0.76rem;
      padding-left: 0.2rem;
    }
  }
}
</style>
